package com.xnxy.design.util;

import com.xnxy.design.model.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 获取常用对象的工具方法
 *
 * @author puzenghcneg
 * @author 2018/10/19
 */
@Configuration
public class SessionUtil {


    /**
     * 获取当前登录用户方法
     *
     * @return
     */
    public static User getUser() {
        HttpServletRequest request = gerReq();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        return user;
    }

    public static HttpSession getSession(){
        return gerReq().getSession();
    }

    /**
     * 获取request工具方案
     *
     * @return
     */
    public static HttpServletRequest gerReq() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 获取response工具方案
     *
     * @return
     */
    public static HttpServletResponse getRep() {
        HttpServletResponse resp = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return resp;
    }


    /**
     * 获取用户ip方法 -》待修改
     *
     * @return
     */
    public static String getIP() {
        HttpServletRequest request = gerReq();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String url = request.getRequestURL().toString();
        return ip;
    }

    /**
     * 当前请求的项目地址
     *
     * @return
     */
    public static String getUrl(String cururl) {
        String regex = "^.*?://(?:[a-zA-Z]+[.])?(\\w+([.]\\w+)*)/.*$";
        Matcher matcher = Pattern.compile(regex).matcher(cururl);
        String result = null;
        if (matcher.find()) {
            result = matcher.group(1);
        }
        System.out.println(result);
//        return "http://"+result;
        return "http://127.0.0.1:80";
//        return "http://10.253.113.112/srv/router/api/exportal";
    }

    public static void doGet() {
        HttpServletRequest request = gerReq();
        HttpServletResponse response = getRep();
        String uri = request.getRequestURI();//返回请求行中的资源名称
        String url = request.getRequestURL().toString();//获得客户端发送请求的完整url
        String ip = request.getRemoteAddr();//返回发出请求的IP地址
        String params = request.getQueryString();//返回请求行中的参数部分
        String host = request.getRemoteHost();//返回发出请求的客户机的主机名
        int port = request.getRemotePort();//返回发出请求的客户机的端口号。
        System.out.println(ip);
        System.out.println(url);
        System.out.println(uri);
        System.out.println(params);
        System.out.println(host);
        System.out.println(port);
    }

}
